package com.mail.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mail.dubbo.mapper.SmsHomeAdvertiseMapper;
import com.mail.model.db.SmsHomeAdvertise;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author yan
 * @date 2022/06/23 14:30
 **/
@DubboService
public class SmsHomeAdvertiseApiImpl implements SmsHomeAdvertiseApi {
    @Autowired
    private SmsHomeAdvertiseMapper mapper;

    @Override
    public void create(SmsHomeAdvertise advertise) {
        mapper.insert(advertise);
    }

    @Override
    public void delete(List<Long> ids) {
        mapper.deleteBatchIds(ids);
    }

    @Override
    public Page<SmsHomeAdvertise> list(SmsHomeAdvertise advertise, Long pageNum, Long pageSize, String endTime) {
        LocalDateTime localStartTime = null;
        LocalDateTime localEndTime = null;
        if (endTime != null) {
            LocalDate beginDateTime = LocalDate.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            localStartTime = beginDateTime.atStartOfDay();
            localEndTime = localStartTime.plusDays(1L);
        }
        Page<SmsHomeAdvertise> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeAdvertise> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(advertise.getType() != null, SmsHomeAdvertise::getType, advertise.getType());
        wrapper.between(endTime != null, SmsHomeAdvertise::getEndTime, localStartTime, localEndTime);
        wrapper.like(advertise.getName() != null, SmsHomeAdvertise::getName, advertise.getName());
        mapper.selectPage(page, wrapper);
        return page;
    }

    @Override
    public void updateStatusById(Long id, Integer status) {
        mapper.updateStatusById(id, status);
    }

    @Override
    public void updateById(Long id, SmsHomeAdvertise advertise) {
        mapper.updateById(advertise);
    }

    @Override
    public SmsHomeAdvertise getAdvertiseById(Long id) {

        return mapper.selectById(id);
    }
}
